
Page({
  data: {
    // 当前选择的导航字母
    selected: 0,
    // 选择字母视图滚动的位置id
    scrollIntoView: 'A',
    // 导航字母
    letters: ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
      'U', 'V', 'W', 'X', 'Y', 'Z'],
    groups: [{
      groupName: 'A',
      users: [
        {
          name: '阿'
        }
      ]
    },
    {
      groupName: 'B',
      users: [
        {
          name: '白'
        }
      ]
    },
    {
      groupName: 'C',
      users: [
        {
          name: 'cc'
        }
      ]
    },
    {
      groupName: 'D',
      users: [
        {
          name: '点'
        }
      ]
    },
    {
      groupName: 'F',
      users: [
        {
          name: 'ffffff'
        }
      ]
    },
    {
      groupName: 'G',
      users: [
        {
          name: 'ggg'
        }
      ]
    },
    {
      groupName: 'H',
      users: [
        {
          name: 'hhh'
        }
      ]
    },
    {
      groupName: 'T',
      users: [
        {
          name: 't植物'
        },
        {
          name: 't植物'
        },
        {
          name: 't植物'
        }
      ]
    },
    {
      groupName: 'X',
      users: [
        {
          name: 'x植物'
        },
        {
          name: 'x植物'
        },
        {
          name: 'x植物'
        }
      ]
    }
    ]
  },
  onLoad: function (options) {
    //处理数据函数
    this.dealWithData();
  },
  //读取点击的字母
  tabLetter(e) {
    // console.log(e);
    const index = e.currentTarget.dataset.index;
    this.setData({
      selected: index,
      scrollIntoView: index
    })
    console.log(index);
    this.cleanAcitvedStatus();
  },
  // 清除字母选中状态
  cleanAcitvedStatus() {
    setTimeout(() => {
      this.setData({
        selected: 0
      })
    }, 500);
  },
  //触摸事件
  touchend(e) {
    this.cleanAcitvedStatus();
  },
  //处理返回的数据
  dealWithData:function(){
    var that=this;
    var searchKey = '科';
    var keData;
    var groups = [];
    var nameStr;//关键字数组
    var singleItem = {};//首字母的算一个项目
    var length = 0;//关键字的长度
    var i = 0;//循环控制变量
    var j = 0;
    var userLength = 0;//搜字母相同的全部科的长度
    var nameData = {};//一个科对应一个对象
    var keDataName = [];//搜字母相同的全部科
    var userData = [];//搜字母相同的全部科的对应的nameData的集合
    var keShuUrl = getApp().globalData.keShuUrl + searchKey;
    console.log(keShuUrl);
    wx.request({
      url: keShuUrl,
      success: function (res) {
        keData = res.data;
        console.log(keData);
        nameStr = keData['keyStr'];
        length = nameStr.length; 
        console.log('关键字的长度'+length);
        for(i=0;i<length;i++)
        {
          singleItem={};
          singleItem['groupName'] = nameStr[i];
          userData=[];
          keDataName = keData[nameStr[i]];
          userLength = keDataName.length;
          for(j=0;j<userLength;j++)
          {
            nameData = {};
            nameData['name'] = keDataName[j];
            userData.push(nameData);
          }
          singleItem['users'] = userData;
          groups.push(singleItem);
          console.log(groups);
          that.setData({groups:groups});
        }
      }
    })
  }
})